All files / util sorted_set.ts

91.14% Statements 72/79
81.25% Branches 13/16
91.3% Functions 21/23
95.77% Lines 68/71
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155                                2x                 2x     14467x 14467x             2x 1x 1x 3x   1x     2x 9177x     2x 1x     2x 1x     2x 2018x     2x 7x       2x 15475x 5957x 5957x         2x 2091x 2091x 1113x 1113x 915x             2x   122x 121x   1x   122x 28x 28x 28x         2x 1848x 1848x       2x 9034x       2x 7257x 1669x     2x 1625x     2x 727x 727x 106x   727x     2x 62x 62x   62x 62x 62x 40x 40x 40x   62x     2x           2x 10703x 10703x 10703x   2x  
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { SortedMap, SortedMapIterator } from './sorted_map';
 
/**
 * SortedSet is an immutable (copy-on-write) collection that holds elements
 * in order specified by the provided comparator.
 *
 * NOTE: if provided comparator returns 0 for two elements, we consider them to
 * be equal!
 */
export class SortedSet<T> {
  private data: SortedMap<T, boolean>;
 
  constructor(private comparator: (left: T, right: T) => number) {
    this.data = new SortedMap<T, boolean>(this.comparator);
  }
 
  /**
   * Creates a SortedSet from the keys of the map.
   * This is currently implemented as an O(n) copy.
   */
  static fromMapKeys<K, V>(map: SortedMap<K, V>): SortedSet<K> {
    let keys = new SortedSet<K>(map.comparator);
    map.forEach(key => {
      keys = keys.add(key);
    });
    return keys;
  }
 
  has(elem: T): boolean {
    return this.data.get(elem) !== null;
  }
 
  first(): T | null {
    return this.data.minKey();
  }
 
  last(): T | null {
    return this.data.maxKey();
  }
 
  get size(): number {
    return this.data.size;
  }
 
  indexOf(elem: T): number {
    return this.data.indexOf(elem);
  }
 
  /** Iterates elements in order defined by "comparator" */
  forEach(cb: (elem: T) => void): void {
    this.data.inorderTraversal((k: T, v: boolean) => {
      cb(k);
      return false;
    });
  }
 
  /** Iterates over `elem`s such that: range[0] <= elem < range[1]. */
  forEachInRange(range: [T, T], cb: (elem: T) => void): void {
    const iter = this.data.getIteratorFrom(range[0]);
    while (iter.hasNext()) {
      const elem = iter.getNext();
      if (this.comparator(elem.key, range[1]) >= 0) return;
      cb(elem.key);
    }
  }
 
  /**
   * Iterates over `elem`s such that: start <= elem until false is returned.
   */
  forEachWhile(cb: (elem: T) => boolean, start?: T): void {
    let iter: SortedMapIterator<T, boolean>;
    if (start !== undefined) {
      iter = this.data.getIteratorFrom(start);
    } else {
      iter = this.data.getIterator();
    }
    while (iter.hasNext()) {
      const elem = iter.getNext();
      const result = cb(elem.key);
      if (!result) return;
    }
  }
 
  /** Finds the least element greater than or equal to `elem`. */
  firstAfterOrEqual(elem: T): T | null {
    const iter = this.data.getIteratorFrom(elem);
    return iter.hasNext() ? iter.getNext().key : null;
  }
 
  /** Inserts or updates an element */
  add(elem: T): SortedSet<T> {
    return this.copy(this.data.remove(elem).insert(elem, true));
  }
 
  /** Deletes an element */
  delete(elem: T): SortedSet<T> {
    if (!this.has(elem)) return this;
    return this.copy(this.data.remove(elem));
  }
 
  isEmpty(): boolean {
    return this.data.isEmpty();
  }
 
  unionWith(other: SortedSet<T>): SortedSet<T> {
    let result: SortedSet<T> = this;
    other.forEach(elem => {
      result = result.add(elem);
    });
    return result;
  }
 
  isEqual(other: SortedSet<T>): boolean {
    Iif (!(other instanceof SortedSet)) return false;
    Iif (this.size !== other.size) return false;
 
    const thisIt = this.data.getIterator();
    const otherIt = other.data.getIterator();
    while (thisIt.hasNext()) {
      const thisElem = thisIt.getNext().key;
      const otherElem = otherIt.getNext().key;
      Iif (this.comparator(thisElem, otherElem) !== 0) return false;
    }
    return true;
  }
 
  toString(): string {
    const result: T[] = [];
    this.forEach(elem => result.push(elem));
    return 'SortedSet(' + result.toString() + ')';
  }
 
  private copy(data: SortedMap<T, boolean>): SortedSet<T> {
    const result = new SortedSet(this.comparator);
    result.data = data;
    return result;
  }
}